<?php

class Database{

	protected $config;
	protected $db_driver;

	public function __construct(){
		global $config;
		
		//Initialize Variables
		$this->config = $config;
		//load db driver
		$this->db_driver = $this->getDatabaseDriver();
	}
	
	
	// High order methods
	public function get_row($table, $getByColum, $getByValue){
			return $this->db_driver->get_row($table, $getByColum, $getByValue);// table, get by colum, get by value
	}
	
	public function  get_rows($table,$valuesToReturn,$start=0,$limit=50){
			return $this->db_driver->get_rows($table,$valuesToReturn,$start,$limit);
	}
		
	//We has all possible feilds know to us since we got this all from db anyway, hence we know where everything goes
	public function set_row($table,$getByColum,$useValue,$keyed_value_array){
		return  $this->db_driver->set_row($table,$getByColum,$useValue,$keyed_value_array);
	}
	//create_row('users',array('new','data','andsuch'))
	public function create_row($table,$values_array){
		//make user

		return $this->db_driver->create_row($table,$values_array);
	}
		
	//Low order methods
	public function query($sql){
		return $this->db_driver->query($sql);
	}
	
	public function fetchRow(){
		return $this->db_driver->fetchRow();
	}
	
	public function countRows(){
		return $this->db_driver->countRows();
	}
	
	public function cleanString($str){
		return $this->db_driver->cleanString($str);
	}
	

	
	
	/*
	CreateDatabase 
	Configure and load specifed database Object
	*/
	private function getDatabaseDriver(){
		if($this->config['database_driver']=='flat'){
			//Flat file
			$dbOpt['directory'] = $this->config['data_path'];
			$dbOpt['ext'] 		= $this->config['ext'];
			return new FlatFileDB($dbOpt);
		}else{
			//SQL
			$dbOpt['sql_dbname'] 	= $this->config['sql_dbname'];
			$dbOpt['sql_username'] 	= $this->config['sql_username'];
			$dbOpt['sql_pass'] 		= $this->config['sql_pass'];
			$dbOpt['sql_host'] 		= $this->config['sql_host'];
			$dbOpt['sql_prefix'] 	= $this->config['sql_prefix'];
			$dbOpt['safe_mode'] 	= $this->config['safe_mode'];
			return new SqlDB($dbOpt);
		}
	}







}